package com.zqweb.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zqweb.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    @Value("app_id")
    private   String app_id;

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCtXMlCjnCGIJ8kJk9/kqAP+HUFt2SFFVhmoSBDXpA4FIxAciWbwkPYECuG0YEMseZs8x6N8sGCgTMe6o3eiDfmIcgdYQUGwurJhbjIsxuSNU9C6+CvvFQrdBHjvoY8bSvApZ+A7Z3MOg1BLqwSCjDM+FThLTlKaoHGEj7tcklhvmDwu0XLgwm42/6Dsn8yWSKWGjUPSkT2KyxK4tvtUUZzINKa6VX20EOANQIHc5tjsf1M0F67WLMICHzr/5GtNDuh70gLHpXqUygxYuOLjPGH9pJCbk/Lujpq3xyaXniM0Ah/YoWLQ6TyEfXGkFcjSkP/AKdZ+jn5uxfZ9k8y1E69AgMBAAECggEALthMtgRhWY0FE7KadsF7KPWdQX8t1VxQN+NAIZQy6kR7JYryqKUUyhSMfnCsK28C8du8HOVE7uvUkX0V+tTaLgBg37dwsUQIR44vwWPMhnSLR20PmM0N/XPuxZidE0T4tqTKdf8/TYk+PafL7U/9odVUskgTHA2tpYUWpfjk5VknSb+A62ru1z0HD8BUc4H9mhniF0O8dUQNfgmgEdPVvRx7GQvsWPVG16Tx2vDPBKTGNElN7ynZRizhLuc5ytweadJY81oQpng3tqkDJBBbjmHzt7N8+LySUqXin6QLc4/3zkS6pgFqWsKrL439tZgPDWEQuX9goFqrAMA1tw9NIQKBgQDb5/DQ3FjNA+P/w527K3uSEV/7boj0xlrCS6vtmjpjS/sTdn7dU0MZ8puzTbPBJ3Y33ep3lsnKPE0hUkhmJGzmPeCjICkKvr4THDHSZHXFBAIXHtp/pnxD2jHw6XN4+DnhtbkezUWJGArYVNub11HRSSt6TnQfuBGlTcfH8q4fyQKBgQDJ0SrePy5AjLmvjKuRKKSBYbyxvXeT2t0cHx36fWrwulHvdfYjgnechbHNl6rOJMKTLvsmYXbVe3wbotiCg61PFsPLcnu1hLR+EEBvwE065yE4fNquRaFCkjwT+cuKeofoOBUzTB9SxGbBloXNyrkxrmLfFyyAwL1c0HQ6YnA5VQKBgQC2IWCjK4xkqG9CmrpdlcLvlHJhZQgLaukqeM/5pz3qlbhqQz8JgfdWW0g3dgiF1nAkDyktSPbyB4V+Lo8GAe8rtl4B3RYA7z+vxi9BaLDdrtcoXQZsqO/Mpr+sHz3jF/ughAjBiROpWv9Icue8J9Z8ttRKpQWqa6I5vbTUVpt+eQKBgDiQJzOjAZOV3XUWN/+3JGQewIQQVjNaZB49mmlaPV662bwDk7jgGCYGUo8MsxKTuvUFdd6L8oJi2aXlPklWHnzMJJQgwgtd5jhu1aB8hSi9HtJ2K9QsL8Y4th3jJOjq7qxnC4oVC7Jm2WXoihfjeYQzHALqu85aNriMtAapEEsBAoGBAL8BDjgYF0CZWXN382XpLUPxvdR9YtLlZFZerHo8/5sZjartGSvKy7q9ihbGmxoyKl4ACOzoGIRw/eBunKRTLJjtjkGQempUl6GRk1ORVN+JK6FhcvdWYsO77+/krxjl7mP9lbwMaUp7S4PlCiX2tgbSmV/WdM4yhDf3X3PvRIYD";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkZaMwvnQp0zAFeoXQMfthPjZLb6IT8Q2H502qqnIgO/t/3QtRI8Q/YnmZCQMRoCar34S56gn5ckNUIhg5leExJJLOLRcPjLU/bEBKgXqPcYq8c0jwW+oaQM6B8bdu4cBXQyjCWnQoQVlr385agffzKyaSKSwir8OMjVdC9Fm252mJhz6wwp2P6EPYG1A2yklX9YUsMvkZud2Um0BvtP/1VdUtBa9AUlENzr3NnmUTos07RjidNlO1wPREJ97MPh3Iv7zXGFZum9XOqOFbBf9gqmlMcFG20xgGSeX4GhXj+9LsuAjjchKpNh0wX3BU8Axa02Z5JpkMX/GY1wKHHgeQQIDAQAB";
    //    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息（必须是外网可访问）
    private  String notify_url = "http://48234dh450.qicp.vip/payed/notify";// 配置成内网穿透的外网访问域名地址

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    // 支付超时的时间
    private String ttl = "30m";// 30min


    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+  ttl +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
